/*
 * Copyright (c) 2010, artur
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * kiddie-os
 * Created on: 14.04.2010
 *     Author: artur
 *
 */

#include <kiddie/kiddie.h>
#include <sys/types.h>
#include <x86/ArchController.h>
#include <irq/EventController.h>

namespace kernel {
namespace irq {

ArchController::ArchController() {
	// TODO Auto-generated constructor stub
}

ArchController::~ArchController() {
	// TODO Auto-generated destructor stub
}


void ArchController::irq_enable(void)
{
	interrupt_enable();
}

void ArchController::irq_disable(void)
{
	interrupt_disable();
}


/* 'del' IRQ mask */
ArchController &ArchController::operator +(unsigned int mask)
{
#warning "Not implemented"
	return *this;
}

/* 'add' IRQ mask */
ArchController &ArchController::operator -(unsigned int mask)
{
#warning "Not implemented"
	return *this;
}

unsigned int ArchController::irq_translate(struct cpu_regs *regs)
{
	/* FIXME Event mapping must be done */
	#warning "Event mapping must be done"
	if (regs->trap_no == 0) return kernel::irq::Event::TimerEvent; 
	if (regs->trap_no == 1) return kernel::irq::Event::KeyboardEvent;
	if (regs->trap_no == 3) return kernel::irq::Event::Serial1Event;
	if (regs->trap_no == 14) return kernel::irq::Event::PageFaultEvent;
	return regs->trap_no;
}

/*
 * Initialize 8259 interrupt controllers.
 * All interrupts will be masked off in ICU.
 * This is called by interrupt_init() from 'sys/irq.c'
 */
void ArchController::archinit(void)
{
	irq_remap();
	return;
}


/* TODO Move this to platform header! */
#define PIC_MA	0x20
#define PIC_SL	0xA0
/**
 * Normally, IRQs 0 to 7 are mapped to entries 8 to 15. This
 * is a problem in protected mode, because IDT entry 8 is a
 * Double Fault! Without remapping, every time IRQ0 fires,
 * you get a Double Fault Exception, which is NOT actually
 * what's happening. We send commands to the Programmable
 * Interrupt Controller (PICs - also called the 8259's) in
 * order to make IRQ0 to 15 be remapped to IDT entries 32 to 47
 */
void ArchController::irq_remap(void)
{
    __outb(0x11,PIC_MA);
    __outb(0x20,PIC_MA + 1);
    __outb(0x04,PIC_MA + 1);
    __outb(0x01,PIC_MA + 1);
    __outb(0x0, PIC_MA + 1);

    __outb(0x11,PIC_SL);
    __outb(0x28,PIC_SL + 1);
    __outb(0x02,PIC_SL + 1);
    __outb(0x01,PIC_SL + 1);
    __outb(0x0, PIC_SL + 1);

    return;
}

/**
 * FIXME not just timer must be cleared!!!
 * TODO Create correct IRQ translator
 */
void ArchController::irq_received(unsigned int ivecno)
{
    /*
     * TODO : Do correct check of the vector number
     *
     * FIXME!!
     * If the IDT entry that was invoked was greater than 8
     * (meaning IRQ8 - 15), then we need to send an EOI to
     * the slave controller
     */
    if (ivecno >= 8)
    {
        __outb(0x20,PIC_SL);
    }

    /* In either case, we need to send an EOI to the master
    *  interrupt controller too */
    __outb(0x20, PIC_MA);

}


}
}
